package com.GameComponent;

import com.Core.GameEngine;
import com.Core.GameLogic;
import com.DataWrapper.EventTracker;
import com.Object.SeaObject;
import com.Triocean.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainGame extends Activity
{	private TextView scoreV;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{	super.onCreate(savedInstanceState);
		if (GameEngine.mapModel==null) GameEngine.mapModel = GameEngine.gameDb.GetMapModel(GameEngine.profile.getCurMapId());
		if (GameEngine.curMap==null) GameEngine.curMap = GameEngine.gameDb.GetMapById(GameEngine.profile.getCurMapId());
		GameEngine.tracker = new EventTracker();
		GameEngine.logic = new GameLogic();
		GameEngine.mainGameAct = this;
		GameEngine.curObj = SeaObject.CreateObject(SeaObject.randomObject());
		
		setContentView(R.layout.game_screen);
		
		scoreV = (TextView)findViewById(R.id.ScoreText);
		UpdateInfo();
		ImageView backBt = (ImageView)findViewById(R.id.backBt);
		backBt.setOnClickListener(new OnClickListener()
		{	@Override
	        public void onClick(View v)
	    	{	new Handler().postDelayed
				(	new Thread()
					{	@Override
						public void run()
						{	GameEngine.gameDb.UpdateMapData();
							Intent titleScreen=new Intent(MainGame.this, TitleScreen.class);
							MainGame.this.startActivity(titleScreen);
							MainGame.this.finish();
							MainGame.this.overridePendingTransition(R.layout.fadein, R.layout.fadestill);
						}
					},
					500
				);
	        }
		});
	}
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{	if (!GameEngine.disableInput)
		{	int width = GameEngine.display.getWidth(), height = GameEngine.display.getHeight();  
			float lx,ly;
			if (width<height)
			{	lx = GameEngine.FIELD_LENGTH;
				ly = lx/width*height;
			}
			else
			{	ly = GameEngine.FIELD_LENGTH;
				lx = ly/height*width;
			}
			
			float x = event.getX()/width*lx+GameEngine.pjLeft;
			float y = (height-event.getY())/height*ly+GameEngine.pjBottom;
			GameEngine.tracker.Set(event.getAction(), x, y);
			if (event.getAction()==MotionEvent.ACTION_UP)
			{	GameEngine.disableInput=true;
			}
		}
		
		return false;
	}
	public void UpdateInfo()
	{	String score = "  "+GameEngine.profile.getScore()+"  ";
		scoreV.setText(score);
	}
	public void GameOver()
	{	Dialog dialog = new Dialog(MainGame.this);
		dialog.setContentView(R.layout.game_over);
		dialog.setTitle("Mission Failed!");
	    dialog.setCancelable(true);
	    
	    TextView text = (TextView) dialog.findViewById(R.id.TextViewGameOver);
	    text.setText("Retry the mission?");
	
	    //set up button
	    Button okBt = (Button)dialog.findViewById(R.id.ButtonOkGameOver);
	    Button cancelBt = (Button)dialog.findViewById(R.id.ButtonCancelGameOver);
	    
	    GameEngine.gameDb.ResetMapData();
	    GameEngine.mapModel = GameEngine.gameDb.GetMapModel(GameEngine.profile.getCurMapId());
		GameEngine.curMap = GameEngine.gameDb.GetMapById(GameEngine.profile.getCurMapId());
		GameEngine.gameDb.UpdateProfie();
	    
	    okBt.setOnClickListener(new OnClickListener()
	    {	@Override
	        public void onClick(View v)
	    	{	new Handler().postDelayed
				(	new Thread()
					{	@Override
						public void run()
						{	Intent titleScreen=new Intent(MainGame.this, MainGame.class);
							MainGame.this.startActivity(titleScreen);
							MainGame.this.finish();
							MainGame.this.overridePendingTransition(R.layout.fadein, R.layout.fadestill);
						}
					},
					500
				);
	        }
	    });
	    cancelBt.setOnClickListener(new OnClickListener()
	    {	@Override
	        public void onClick(View v)
	    	{	new Handler().postDelayed
				(	new Thread()
					{	@Override
						public void run()
						{	Intent titleScreen=new Intent(MainGame.this, TitleScreen.class);
							MainGame.this.startActivity(titleScreen);
							MainGame.this.finish();
							MainGame.this.overridePendingTransition(R.layout.fadein, R.layout.fadestill);
						}
					},
					500
				);
	        }
	    });
	    dialog.show();
	}
	public void FinishStage()
	{	Dialog dialog = new Dialog(MainGame.this);
	    dialog.setContentView(R.layout.end_stage);
	    dialog.setTitle("Mission Complete!");
	    dialog.setCancelable(true);
	
	    //set up text
	    TextView text = (TextView) dialog.findViewById(R.id.TextViewEndStage);
	    text.setText("To the next stage?");
	
	    //set up button
	    Button okBt = (Button)dialog.findViewById(R.id.ButtonOkEndStage);
	    Button cancelBt = (Button)dialog.findViewById(R.id.ButtonCancelEndStage);
	    
	    GameEngine.gameDb.UpdateMapData();
	    GameEngine.curMap = GameEngine.gameDb.GetNextMap();
		GameEngine.profile.setCurMapId(GameEngine.curMap.getId());
		GameEngine.mapModel = GameEngine.gameDb.GetMapModel(GameEngine.profile.getCurMapId());
		GameEngine.gameDb.UpdateProfie();
	    
	    okBt.setOnClickListener(new OnClickListener()
	    {	@Override
	        public void onClick(View v)
	    	{	new Handler().postDelayed
				(	new Thread()
					{	@Override
						public void run()
						{	Intent titleScreen=new Intent(MainGame.this, MainGame.class);
							MainGame.this.startActivity(titleScreen);
							MainGame.this.finish();
							MainGame.this.overridePendingTransition(R.layout.fadein, R.layout.fadestill);
						}
					},
					500
				);
	        }
	    });
	    cancelBt.setOnClickListener(new OnClickListener()
	    {	@Override
	        public void onClick(View v)
	    	{	new Handler().postDelayed
				(	new Thread()
					{	@Override
						public void run()
						{	Intent titleScreen=new Intent(MainGame.this, TitleScreen.class);
							MainGame.this.startActivity(titleScreen);
							MainGame.this.finish();
							MainGame.this.overridePendingTransition(R.layout.fadein, R.layout.fadestill);
						}
					},
					500
				);
	        }
	    });
	    dialog.show();
	}
	public void HandleEndGame(final int code)
	{	this.runOnUiThread(new Runnable() {
	    	public void run() {
	    		if (code==0) FinishStage();
	    		if (code==1) GameOver();
	    	}
		});
	}

}
